<template>
  <div class="login-container">
    <!-- Background decorations -->
    <div class="login-background">
      <div class="shape shape1"></div>
      <div class="shape shape2"></div>
      <div class="shape shape3"></div>
      <div class="particles"></div>
    </div>

    <div class="login-box">
      <!-- Left illustration area -->
      <div class="illustration-box">
        <h2>加入信息系统</h2>
        <p>开启高效管理之旅</p>
      </div>

      <!-- Right registration form -->
      <div class="login-form">
        <div class="form-header">
          <h1>用户注册</h1>
          <p>创建您的账号</p>
        </div>

        <el-form
          ref="registerForm"
          :model="registerForm"
          :rules="registerRules"
          @keyup.enter.native="handleRegister"
        >
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
            ></el-input>
          </el-form-item>

          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              placeholder="请确认密码"
              prefix-icon="el-icon-lock"
              show-password
            ></el-input>
          </el-form-item>

          <div class="login-options">
            <el-checkbox v-model="agreeTerms">
              我已阅读并同意
              <el-button type="text" @click="showTerms">《用户协议》</el-button>
            </el-checkbox>
          </div>

          <el-button
            :loading="loading"
            type="primary"
            class="login-button"
            @click="handleRegister"
          >
            {{ loading ? "注册中..." : "注 册" }}
          </el-button>

          <div class="login-footer">
            已有账号？
            <el-button type="text" @click="goToLogin">立即登录</el-button>
          </div>
        </el-form>
      </div>
    </div>

    <!-- Terms Dialog -->
    <el-dialog title="用户协议" :visible.sync="termsVisible" width="70%">
      <div class="terms-content">
        <h3>1. 服务条款的接受</h3>
        <p>
          通过完成注册程序并使用本服务，您表示已充分阅读、理解并同意接受本协议的条款和条件。
        </p>

        <h3>2. 用户账户</h3>
        <p>
          您需要注册账户才能使用我们的服务。您必须提供真实、准确、完整和最新的注册信息。
        </p>

        <h3>3. 用户义务</h3>
        <p>您同意不会使用本服务进行任何非法活动或侵犯他人权利的行为。</p>

        <h3>4. 隐私政策</h3>
        <p>我们将按照隐私政策处理您的个人信息，请仔细阅读我们的隐私政策。</p>

        <h3>5. 服务变更</h3>
        <p>我们保留随时修改或中断服务的权利，恕不另行通知。</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="
            agreeTerms = true;
            termsVisible = false;
          "
          >同意并继续</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { userregister } from "@/api/user";
export default {
  name: "RegisterPage",
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
      },
      registerRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 5,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          { required: true, message: "请确认密码", trigger: "blur" },
          { validator: validatePassword, trigger: "blur" },
        ],
      },
      loading: false,
      agreeTerms: false,
      termsVisible: false,
    };
  },
  methods: {
    async handleRegister() {
      try {
        await this.$refs.registerForm.validate();

        if (!this.agreeTerms) {
          this.$message.warning("请先同意用户协议");
          return;
        }

        this.loading = true;
        const response = await userregister(
          this.registerForm.username,
          this.registerForm.password
        );
        console.log(response);

        if (response.status) {
          this.$message.success("注册成功！");
          this.$router.push("/login");
        } else {
          this.$message.error(response.message || "注册失败");
        }
      } catch (error) {
        console.error("注册失败:", error);
        // if (error.response && error.response.data.message) {
        //   this.$message.error(error.response.data.message);
        // } else {
        //   this.$message.error("注册失败，请检查输入");
        // }
      } finally {
        this.loading = false;
      }
    },

    showTerms() {
      this.termsVisible = true;
    },

    goToLogin() {
      this.$router.push("/login");
    },
  },
};
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  background: #f0f2f5;
  overflow: hidden;
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 0;
  overflow: hidden;

  .shape {
    position: absolute;
    border-radius: 50%;
    background: linear-gradient(
      45deg,
      rgba(64, 158, 255, 0.1),
      rgba(24, 144, 255, 0.1)
    );
  }

  .shape1 {
    width: 600px;
    height: 600px;
    top: -200px;
    right: -100px;
  }

  .shape2 {
    width: 400px;
    height: 400px;
    bottom: -150px;
    left: -150px;
  }

  .shape3 {
    width: 300px;
    height: 300px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    opacity: 0.1;
  }

  .particles {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: radial-gradient(
        circle at 25% 25%,
        rgba(64, 158, 255, 0.1) 1px,
        transparent 1px
      ),
      radial-gradient(
        circle at 75% 75%,
        rgba(24, 144, 255, 0.1) 1px,
        transparent 1px
      );
    background-size: 50px 50px;
    animation: particleMove 20s linear infinite;
  }
}

.login-box {
  position: relative;
  z-index: 1;
  width: 900px;
  height: 560px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  display: flex;
  overflow: hidden;
  transition: transform 0.3s ease;

  &:hover {
    transform: translateY(-5px);
  }
}

.illustration-box {
  flex: 1;
  background: linear-gradient(135deg, #409eff 0%, #1890ff 100%);
  padding: 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #fff;
  text-align: center;

  img {
    width: 90%;
    max-width: 400px;
    margin-bottom: 30px;
    animation: float 6s ease-in-out infinite;
  }

  h2 {
    font-size: 28px;
    margin-bottom: 15px;
    font-weight: 500;
  }

  p {
    font-size: 16px;
    opacity: 0.9;
  }
}

.login-form {
  width: 400px;
  padding: 40px;
  display: flex;
  flex-direction: column;

  .form-header {
    text-align: center;
    margin-bottom: 40px;

    h1 {
      font-size: 28px;
      color: #303133;
      margin-bottom: 10px;
    }

    p {
      font-size: 14px;
      color: #909399;
    }
  }

  .el-form-item {
    margin-bottom: 25px;

    .el-input {
      .el-input__inner {
        height: 45px;
        line-height: 45px;
        border-radius: 8px;
        padding-left: 45px;
        border: 1px solid #dcdfe6;
        transition: all 0.3s;

        &:focus {
          border-color: #409eff;
          box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
        }

        &:hover {
          border-color: #409eff;
        }
      }

      .el-input__prefix {
        left: 15px;
        color: #909399;
      }
    }
  }

  .login-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 25px;

    .el-checkbox {
      .el-checkbox__label {
        color: #606266;
      }
    }

    .el-button--text {
      color: #409eff;
      padding: 0;

      &:hover {
        color: #1890ff;
      }
    }
  }

  .login-button {
    height: 45px;
    font-size: 16px;
    border-radius: 8px;
    background: #409eff;
    border: none;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 8px 15px rgba(64, 158, 255, 0.3);
    }

    &:active {
      transform: translateY(0);
    }
  }
}

.login-footer {
  margin-top: 20px;
  text-align: center;
  color: #606266;
  font-size: 14px;

  .el-button {
    padding: 0;
    margin-left: 5px;
  }
}

.terms-content {
  max-height: 60vh;
  overflow-y: auto;
  padding: 10px 20px;
  line-height: 1.6;

  h3 {
    margin-top: 20px;
    color: #409eff;
  }

  p {
    margin: 10px 0;
  }
}

@keyframes float {
  0% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-20px);
  }
  100% {
    transform: translateY(0px);
  }
}

@keyframes particleMove {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 50px 50px;
  }
}

/* Responsive styles */
@media screen and (max-width: 768px) {
  .login-container {
    padding: 15px;
    min-height: 100vh;
    background: #f0f2f5;
    align-items: flex-start;
  }

  .login-box {
    width: 100%;
    height: auto;
    min-height: auto;
    flex-direction: column;
    margin: 0;
    padding: 0;
    transform: none;

    &:hover {
      transform: none;
    }
  }

  .illustration-box {
    padding: 30px 20px;
    height: auto;
    background: linear-gradient(135deg, #409eff 0%, #1890ff 100%);
    border-radius: 20px 20px 0 0;

    img {
      width: 45%;
      max-width: 160px;
      margin-bottom: 15px;
      animation: none;
    }

    h2 {
      font-size: 20px;
      margin-bottom: 5px;
    }

    p {
      font-size: 14px;
      opacity: 0.8;
      margin: 0;
    }
  }

  .login-form {
    width: 100%;
    padding: 25px 20px;
    background: #fff;
    border-radius: 0 0 20px 20px;

    .form-header {
      margin-bottom: 25px;

      h1 {
        font-size: 22px;
        margin-bottom: 5px;
      }

      p {
        font-size: 13px;
      }
    }

    .el-form-item {
      margin-bottom: 20px;

      .el-input__inner {
        height: 42px;
      }
    }

    .login-options {
      margin-bottom: 20px;
    }

    .login-button {
      width: 100%;
      height: 44px;
      margin-top: 10px;
      font-size: 16px;
      border-radius: 8px;

      &:hover {
        transform: none;
        box-shadow: none;
      }

      &:active {
        opacity: 0.9;
      }
    }
  }
}

@media screen and (max-width: 375px) {
  .login-container {
    padding: 10px;
  }

  .illustration-box {
    padding: 20px 15px;

    img {
      width: 40%;
      max-width: 120px;
      margin-bottom: 10px;
    }

    h2 {
      font-size: 18px;
    }

    p {
      font-size: 12px;
    }
  }

  .login-form {
    padding: 20px 15px;

    .form-header {
      margin-bottom: 20px;
    }

    .el-form-item {
      margin-bottom: 15px;
    }

    .login-button {
      height: 42px;
    }
  }
}

@media screen and (max-width: 768px) and (orientation: landscape) {
  .login-container {
    padding: 10px;
    align-items: center;
  }

  .login-box {
    flex-direction: row;
    max-height: 85vh;
  }

  .illustration-box {
    width: 35%;
    padding: 20px 15px;
    border-radius: 20px 0 0 20px;

    img {
      width: 70%;
      max-width: 140px;
      margin-bottom: 10px;
    }
  }

  .login-form {
    width: 65%;
    border-radius: 0 20px 20px 0;
    overflow-y: auto;

    &::-webkit-scrollbar {
      width: 6px;
    }

    &::-webkit-scrollbar-thumb {
      background: rgba(0, 0, 0, 0.1);
      border-radius: 3px;
    }

    .login-button {
      width: 100%;
      margin-bottom: 10px;
    }
  }
}

@media screen and (min-width: 769px) and (max-width: 1024px) {
  .login-box {
    width: 90%;
    max-width: 800px;
  }

  .illustration-box {
    img {
      width: 85%;
      max-width: 320px;
    }
  }
}
</style>
